﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ObTookit.Auto
{
    public class LGCoder:BaseCoder
    {
        public string CreateCode()
        {
            FieldObject pkFO = GetIdObject();
            string text = ReadTemplate(Template);
            text = text.Replace("[#BaseNS#]", AutoObj.BaseNS);
            text = text.Replace("[#ModelNS#]", AutoObj.OmNS);
            text = text.Replace("[#DalNS#]", AutoObj.DaNS);
            text = text.Replace("[#AUTHOR#]", Author);
            text = text.Replace("[#CREATED#]", CreateDate);
            text = text.Replace("[#LgNS#]", AutoObj.LgNS);

            text = text.Replace("[#ClsBaseLG#]", AutoObj.LgBaseCls);
            text = text.Replace("[#ClsName#]", AutoObj.ClsName);
            text = text.Replace("[#ClsBaseSession#]", AutoObj.BaseSession);

            text = text.Replace("[#IdAlias#]", pkFO.Alias);
            text = text.Replace("[#IdName#]", AutoObj.IdName);
            text = text.Replace("[#IdSysType#]", GetIdType(pkFO.Type));

            text = text.Replace("[#FOR_DEFAULT_STRING#]", CreateDefaultAddString());
            text = text.Replace("[#FOR_UPDATE_STRING#]", CreateDefaultUpdateString());
            return text;
        }

        public string CreateDefaultAddString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (FieldObject fo in FieldObjs)
            {
                string isNull = "";
                string setDef = "";
                if (!fo.Nullable && !fo.PrimaryKey)
                {
                    if (!"StringField".Equals(fo.Type, StringComparison.InvariantCultureIgnoreCase))
                    {
                        isNull = "IsNull";
                        if ("BoolField".Equals(fo.Type, StringComparison.InvariantCultureIgnoreCase))
                        {
                            setDef = "Set(false)";
                        }
                        else if ("DateTimeField".Equals(fo.Type, StringComparison.InvariantCultureIgnoreCase))
                        {
                            setDef = "Now()";
                        }
                        else
                        {
                            setDef = "Set(0)";
                        }
                    }
                    else
                    {
                        isNull = "IsNullOrWhiteSpace";
                        setDef = "Empty()";
                    }


                    sb.Append(
                        CodeArgs.RN + CodeArgs.TAB3 + "if(ei." + fo.PublicProp + "." + isNull + ")"
                        + CodeArgs.RN + CodeArgs.TAB3 + "{"
                        + CodeArgs.RN + CodeArgs.TAB4 + "om." + fo.PublicProp + "." + setDef + ";"
                        + CodeArgs.RN + CodeArgs.TAB3 + "}");
                }
            }

            return sb.ToString();

        }

        public string CreateDefaultUpdateString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (FieldObject fo in FieldObjs)
            {
                if (!fo.PrimaryKey)
                {
                    string isNull = "";
                    if (!"StringField".Equals(fo.Type, StringComparison.InvariantCultureIgnoreCase))
                    {
                        isNull = "IsNull";
                    }
                    else
                    {
                        isNull = "IsNullOrWhiteSpace";
                    }

                    sb.Append(
                        CodeArgs.RN + CodeArgs.TAB3 + "if(!ei." + fo.PublicProp + "." + isNull + ")"
                        + CodeArgs.RN + CodeArgs.TAB3 + "{"
                        + CodeArgs.RN + CodeArgs.TAB4 + "om." + fo.PublicProp + ".Set(ei." + fo.PublicProp + ".Value);"
                        + CodeArgs.RN + CodeArgs.TAB3 + "}");
                }

            }
            return sb.ToString();
        }
    }
}
